﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

namespace sdamarketing
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //NavigationPanel.Visible = false;
            if (Session["idUser"] == null || Session["username"] == null)
            {
                AdministrationPanel.Visible = false;
                AccountPanel.Visible = false;
                //MarketingPanel.Visible = false;
                //     NavigationPanel.Visible = false;
            }
            else
            {
                // OVOJ DEL E PODOBRO VO NEKOJ METOD T.E. DA NAPRAVAM METOD 
                // STO KE VRAKA TRUE ILI FALSE DALI DA GO KRIE ILI DA NE GO KRIE LINKOT, PANELOT
                try
                {
                    AdministrationPanel.Visible = false;
                    AddUserPanel.Visible = false;
                    ChangeUserPanel.Visible = false;
                    changePriviligesPanel.Visible = false;



                    DataSet roles = ReturnDataSet.ReturSet("SELECT Panels.name FROM tbr_PanelRoles INNER JOIN Panels on tbr_PanelRoles.idPanel = Panels.idPanel WHERE tbr_PanelRoles.idRole =" + Session["idRole"].ToString());
                    for (int i = 0; i < roles.Tables["DataSet"].Rows.Count; i++)
                    {
                        if (roles.Tables["DataSet"].Rows[i].ItemArray[0].ToString() == "Администрација")
                        {
                            AdministrationPanel.Visible = true;
                        }
                        else if (roles.Tables["DataSet"].Rows[i].ItemArray[0].ToString() == "Додади корисник")
                        {
                            AddUserPanel.Visible = true;
                        }
                        else if (roles.Tables["DataSet"].Rows[i].ItemArray[0].ToString() == "Менаџирај корисници")
                        {
                            ChangeUserPanel.Visible = true;
                        }
                        else if (roles.Tables["DataSet"].Rows[i].ItemArray[0].ToString() == "Додај/Измени привилегија")
                        {
                            changePriviligesPanel.Visible = true;
                        }

                    }
                }
                catch (Exception exception)
                {
                    SendToLogFile.SendRecordToFIle(DateTime.Now.ToString() + " -exc- " + exception.Message.ToString() + " -stackTrace- " + exception.StackTrace.ToString() + " -MasterPage- ");
                }
               
            }


            HttpResponse r = Response;                    // Get reference to Response
            foreach (DateTime d in TimerExample.DateList) // Get the timer results
            {
                refreshReklami();
            }

        }


        public void zgolemiKlik(int idReklama)
        {
            reklamaEditor re = new reklamaEditor();
            int brKlikovi = (re.getBrNaKlikovi(idReklama)) + 1;
            SqlConnection connection = ConnectionMenager.GetConnection();

            String sql = "UPDATE PostavenaReklama SET brKlikovi='" + brKlikovi + "' WHERE idReklama = '" + idReklama + "'";

            SqlCommand command = new SqlCommand(sql, connection);

            command.ExecuteNonQuery();
            connection.Close();
        }
        
        public void refreshReklami()
        {            
            reklamiZaKorisnici reklami = new reklamiZaKorisnici();
            if (Session["idUser"] != null)
            {
                int user = Int32.Parse(Session["idUser"].ToString());
                DataSet ds = reklami.getPodatociZaReklama(user);

                int brReklami = ds.Tables[0].Rows.Count;

                switch (brReklami)
                {
                    case 0:
                        break;
                    case 1:
                        idReklama1.Text = ds.Tables[0].Rows[0]["idReklama"].ToString();
                        naslovLink1.Text = ds.Tables[0].Rows[0]["Naslov"].ToString();
                        naslovLink1.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        slikaLink1.ImageUrl = ds.Tables[0].Rows[0]["SlikaURL"].ToString();
                        slikaLink1.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        opisLink1.Text = ds.Tables[0].Rows[0]["Opis"].ToString();
                        opisLink1.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();

                        idReklama2.Text = ds.Tables[0].Rows[0]["idReklama"].ToString();
                        naslovLink2.Text = ds.Tables[0].Rows[0]["Naslov"].ToString();
                        naslovLink2.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        slikaLink2.ImageUrl = ds.Tables[0].Rows[0]["SlikaURL"].ToString();
                        slikaLink2.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        opisLink2.Text = ds.Tables[0].Rows[0]["Opis"].ToString();
                        opisLink2.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();

                        idReklama3.Text = ds.Tables[0].Rows[0]["idReklama"].ToString();
                        naslovLink3.Text = ds.Tables[0].Rows[0]["Naslov"].ToString();
                        naslovLink3.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        slikaLink3.ImageUrl = ds.Tables[0].Rows[0]["SlikaURL"].ToString();
                        slikaLink3.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        opisLink3.Text = ds.Tables[0].Rows[0]["Opis"].ToString();
                        opisLink3.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        break;
                    case 2:
                        idReklama1.Text = ds.Tables[0].Rows[0]["idReklama"].ToString();
                        naslovLink1.Text = ds.Tables[0].Rows[0]["Naslov"].ToString();
                        naslovLink1.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        slikaLink1.ImageUrl = ds.Tables[0].Rows[0]["SlikaURL"].ToString();
                        slikaLink1.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        opisLink1.Text = ds.Tables[0].Rows[0]["Opis"].ToString();
                        opisLink1.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();

                        idReklama2.Text = ds.Tables[0].Rows[1]["idReklama"].ToString();
                        naslovLink2.Text = ds.Tables[0].Rows[1]["Naslov"].ToString();
                        naslovLink2.NavigateUrl = ds.Tables[0].Rows[1]["Link"].ToString();
                        slikaLink2.ImageUrl = ds.Tables[0].Rows[1]["SlikaURL"].ToString();
                        slikaLink2.NavigateUrl = ds.Tables[0].Rows[1]["Link"].ToString();
                        opisLink2.Text = ds.Tables[0].Rows[1]["Opis"].ToString();
                        opisLink2.NavigateUrl = ds.Tables[0].Rows[1]["Link"].ToString();

                        idReklama3.Text = ds.Tables[0].Rows[0]["idReklama"].ToString();
                        naslovLink3.Text = ds.Tables[0].Rows[0]["Naslov"].ToString();
                        naslovLink3.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        slikaLink3.ImageUrl = ds.Tables[0].Rows[0]["SlikaURL"].ToString();
                        slikaLink3.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        opisLink3.Text = ds.Tables[0].Rows[0]["Opis"].ToString();
                        opisLink3.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        break;
                    case 3:
                        idReklama1.Text = ds.Tables[0].Rows[0]["idReklama"].ToString();
                        naslovLink1.Text = ds.Tables[0].Rows[0]["Naslov"].ToString();
                        naslovLink1.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        slikaLink1.ImageUrl = ds.Tables[0].Rows[0]["SlikaURL"].ToString();
                        slikaLink1.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();
                        opisLink1.Text = ds.Tables[0].Rows[0]["Opis"].ToString();
                        opisLink1.NavigateUrl = ds.Tables[0].Rows[0]["Link"].ToString();

                        idReklama2.Text = ds.Tables[0].Rows[1]["idReklama"].ToString();
                        naslovLink2.Text = ds.Tables[0].Rows[1]["Naslov"].ToString();
                        naslovLink2.NavigateUrl = ds.Tables[0].Rows[1]["Link"].ToString();
                        slikaLink2.ImageUrl = ds.Tables[0].Rows[1]["SlikaURL"].ToString();
                        slikaLink2.NavigateUrl = ds.Tables[0].Rows[1]["Link"].ToString();
                        opisLink2.Text = ds.Tables[0].Rows[1]["Opis"].ToString();
                        opisLink2.NavigateUrl = ds.Tables[0].Rows[1]["Link"].ToString();

                        idReklama3.Text = ds.Tables[0].Rows[2]["idReklama"].ToString();
                        naslovLink3.Text = ds.Tables[0].Rows[2]["Naslov"].ToString();
                        naslovLink3.NavigateUrl = ds.Tables[0].Rows[2]["Link"].ToString();
                        slikaLink3.ImageUrl = ds.Tables[0].Rows[2]["SlikaURL"].ToString();
                        slikaLink3.NavigateUrl = ds.Tables[0].Rows[2]["Link"].ToString();
                        opisLink3.Text = ds.Tables[0].Rows[2]["Opis"].ToString();
                        opisLink3.NavigateUrl = ds.Tables[0].Rows[2]["Link"].ToString();
                        break;
                    default:
                        int r1, r2, r3;
                        Random rand = new Random();
                        r1 = rand.Next(0, brReklami - 1);
                        do
                        {
                            r2 = rand.Next(0, brReklami - 1);
                        }
                        while (r1 == r2);
                        do
                        {
                            r3 = rand.Next(0, brReklami - 1);
                        }
                        while (r3 == r2 || r3 == r1);

                        idReklama1.Text = ds.Tables[0].Rows[r1]["PostavenaReklama.idReklama"].ToString();
                        naslovLink1.Text = ds.Tables[0].Rows[r1]["Naslov"].ToString();
                        naslovLink1.NavigateUrl = ds.Tables[0].Rows[r1]["Link"].ToString();
                        slikaLink1.ImageUrl = ds.Tables[0].Rows[r1]["SlikaURL"].ToString();
                        slikaLink1.NavigateUrl = ds.Tables[0].Rows[r1]["Link"].ToString();
                        opisLink1.Text = ds.Tables[0].Rows[r1]["Opis"].ToString();
                        opisLink1.NavigateUrl = ds.Tables[0].Rows[r1]["Link"].ToString();

                        idReklama2.Text = ds.Tables[0].Rows[r2]["PostavenaReklama.idReklama"].ToString();
                        naslovLink2.Text = ds.Tables[0].Rows[r2]["Naslov"].ToString();
                        naslovLink2.NavigateUrl = ds.Tables[0].Rows[r2]["Link"].ToString();
                        slikaLink2.ImageUrl = ds.Tables[0].Rows[r2]["SlikaURL"].ToString();
                        slikaLink2.NavigateUrl = ds.Tables[0].Rows[r2]["Link"].ToString();
                        opisLink2.Text = ds.Tables[0].Rows[r2]["Opis"].ToString();
                        opisLink2.NavigateUrl = ds.Tables[0].Rows[r2]["Link"].ToString();

                        idReklama3.Text = ds.Tables[0].Rows[r3]["PostavenaReklama.idReklama"].ToString();
                        naslovLink3.Text = ds.Tables[0].Rows[r3]["Naslov"].ToString();
                        naslovLink3.NavigateUrl = ds.Tables[0].Rows[r3]["Link"].ToString();
                        slikaLink3.ImageUrl = ds.Tables[0].Rows[r3]["SlikaURL"].ToString();
                        slikaLink3.NavigateUrl = ds.Tables[0].Rows[r3]["Link"].ToString();
                        opisLink3.Text = ds.Tables[0].Rows[r3]["Opis"].ToString();
                        opisLink3.NavigateUrl = ds.Tables[0].Rows[r3]["Link"].ToString();
                        break;

                }

            }
            else 
            {
                reklama1.Visible = false;
                reklama2.Visible = false;
                reklama3.Visible = false;
            }

        }



       

    }
}